﻿var Calendar_Year;
var Calendar_Month;

// 设置每个月的日期
function Calendar_SetDate(index, date)
{
	var id = "#index"+index;
	var DateDayFormat = "";
	var DateMonthFormat = "";
	
	if(date.getDate() <10){
		DateDayFormat ="0"+date.getDate();
	}
	else{
		DateDayFormat = ""+date.getDate();
	}

	if((date.getMonth()+1) <10){
		DateMonthFormat ="0"+(date.getMonth()+1);
	}
	else{
		DateMonthFormat =""+(date.getMonth()+1);
	}
	
	var time = ""+date.getFullYear()+"-"+DateMonthFormat+"-"+DateDayFormat;
	var id_Morning = time+"_M";
	var id_Afternoon = time+"_A";
	var showdate;
	if(Calendar_Month == (date.getMonth() + 1)){
		showdate = "<span  style='font-size:30px'>"+date.getDate()+"</span>";
	}
	else{
		showdate = "<span  style='font-size:30px;color:#EEEEEE'>"+date.getDate()+"</span>";
	}
	
	var style = "";
	
	var today = new Date();
	if(today.getFullYear() == date.getFullYear()){
		if(today.getMonth() == date.getMonth()){
			if(today.getDate() == date.getDate()){
				style = "style='background-Color:dfdfdf'"
			}
		}		
	}
	
	var html ="	<table class='Calendar_Data_Item' id='"+time+"' "+ style +">\
				<tr>\
					<td colspan='2' style='height:90%'>"+ showdate +"</td>\
				</tr>\
				<tr>\
					<td id="+id_Morning+"></td>\
					<td id="+id_Afternoon+"></td>\
				</tr>\
				</table>";
				
				
	$(".Calendar").find(id).html(html);	
}

// 初始化日历
function Calendar_Init()
{	
	// 设置当前年月
	var Titlt = "<table width='100%'>\
					<tr><td width='30%'>\
						<button style='width:10px' class='uk-button uk-button-default' id='permonth'><</button>&nbsp\
						<button style='width:10px' class='uk-button uk-button-default' id='nextmonth'>></button>&nbsp\
						<button class='uk-button uk-button-default' id='today'>Today</button>\
					</td>\
					<td style='text-align:center;'><span style='font-size:36px;'>"+Calendar_Year+" 年 "+Calendar_Month+" 月</span></td>\
					<td width='30%'></td></tr>\
					</table>";
					
	$("#CalendarTitle").html(Titlt);
	
	// 判断每月初是星期几
	var MonthFirst = new Date();
	MonthFirst.setFullYear(Calendar_Year);
	MonthFirst.setMonth(Calendar_Month -1);
	MonthFirst.setDate(1);
	var week = MonthFirst.getDay();
	if(week == 0){
		week = 7;
	}
	
	if( i != 1){
		var tmp = new Date(MonthFirst);
		for(var i = week -1 ;i > 0; i--){
			tmp.setDate(tmp.getDate()-1);	
			Calendar_SetDate(i, tmp);				
		}		
	}
	
	// 从7 开始加日期
	for(var i = week ;i < 43; i++){
		Calendar_SetDate(i, MonthFirst);	
		MonthFirst.setDate(MonthFirst.getDate()+1);		
	}
	
	$("#permonth").click(function(){
		Calendar_Month--;
		if(Calendar_Month == 0){
			Calendar_Month = 12;
			Calendar_Year--;
		}
		$("#Calendar_TD").hide();
		Calendar_Init();
		Calendar_UpdateSummary();
	});
	$("#nextmonth").click(function(){
		Calendar_Month ++;
		if(Calendar_Month == 13){
			Calendar_Month = 1;
			Calendar_Year++;
		}
		$("#Calendar_TD").hide();
		Calendar_Init();
		Calendar_UpdateSummary();		
	});
	$("#today").click(function(){
		var today = new Date();
		Calendar_Year = today.getFullYear();
		Calendar_Month = today.getMonth() + 1;
		Calendar_Init();
		Calendar_UpdateSummary();
		$("#Calendar_TD").hide();
	});
	
	$(".Calendar_Data_Item").click(function(e){
		var a = $(e.target).parents(".Calendar_Data_Item").attr("id");
		// 获取数据
		Calendar_UpdateDetail(a);
		});
}

// 刷新日志的简要信息
function Calendar_UpdateSummary()
{
	// 如果没有选中的用户则直接返回
	if(PCList_Select_MAC == ""){
		return;
	}
	$("#Calendar_TD").hide();
	
	// 拼接URL
	var info = "http://121.40.16.218:7789/PCLive/GetPCLiveMonth?year="+Calendar_Year+"&month="+Calendar_Month+"&username="+PCList_Select_UserName+"&mac="+PCList_Select_MAC+"&jsoncallback=?";
	$.getJSON(info,
	function(data){
		if(data.status != "success"){
			return;
		}
	  
		if(data.mac != PCList_Select_MAC){
			return;
		}
		
		// 刷新日历先
		Calendar_Init();
		
		for(var item in data.daysummary) {
			if(data.daysummary[item].a){
				var id_Afternoon = "#"+data.daysummary[item].date+"_A";
				$(id_Afternoon).attr("bgcolor", "#00CC66");
			}
			if(data.daysummary[item].m){
				var id_Morning = "#"+data.daysummary[item].date+"_M";
				$(id_Morning).attr("bgcolor", "#00CC66");
			}
		}
	});
}

// 将时间转换成百分比
function Calendar_TimeToPer(Time, selecttime){
	var arr = Time.split(':');
	return (parseInt(arr[0])*60 + parseInt(arr[1])) / 14.40;
}

// 显示某个日子的详细
function Calendar_UpdateDetail(datetime)
{
	if(PCList_Select_MAC == ""){
		return;
	}
	
	// 接续出年月日
	var arr=datetime.split('-');
	
	var info = "http://121.40.16.218:7789/PCLive/GetPCLiveDay?year="+arr[0]+"&month="+arr[1]+"&username="+PCList_Select_UserName+"&mac="+PCList_Select_MAC+"&day="+arr[2]+"&jsoncallback=?";
	$.getJSON(info,
	function(data){
		if(data.status != "success"){
			return;
		}
	  
		if(data.mac != PCList_Select_MAC){
			return;
		}
		
		if(data.date != datetime){
			return;
		}
		
		// 刷新日历先
		var html ="";
		var htmtxt="<tr><td style='border-width:1px 0px 0px 0px;height:30px'><span style='font-size:30px'>" +datetime+"</span></td></tr>";
			
		for(var item in data.daydetail) {
			var Top = Calendar_TimeToPer(data.daydetail[item].starttime);
			var Height =  Calendar_TimeToPer(data.daydetail[item].shutdowmtime) - Top;
			html += "<div style='height:"+Height+"%; width:10px; position:absolute; left:0; top:"+Top+"%;background-Color:#00CC66'></div>";
			
			htmtxt += "<tr><td style='border-width:1px 0px 0px 0px;'>" +data.daydetail[item].starttime +"-------"+data.daydetail[item].shutdowmtime+"</td></tr>";
		}

				  
		$("#Calendar_Detail_Process").html(html);
		$("#Calendar_Detail_TXT").html(htmtxt);
		$("#Calendar_TD").show();
	});

}

$(function()
{
	var html="<tr height='30px'><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td><td>日</td>\
					<td rowspan='7' id='Calendar_TD' style='border:0;display:none;width:20%;'>\
						<table class='Calendar_Detail' style='height:100%; width:100%;'>\
							<tr>\
								<td width='10px' style='border:0'>\
								<table class='Calendar_Detail_Scale' style='height:100%; width:100%;border:0'>\
									<tr ><td>0</td><tr><tr><td>1</td><tr><td>2</td><tr><td>3</td><tr><td>4</td><tr><td>5</td><tr><td>6</td><tr><td>7</td><tr><td>8</td><tr><td>9</td><tr><td>10</td><tr><td>11</td><tr><td>12</td></tr>\
									<tr><td>13</td><tr><td>14</td><tr><td>15</td><tr><td>16</td><tr><td>17</td><tr><td>18</td><tr><td>19</td><tr><td>20</td><tr><td>21</td><tr><td>22</td><tr><td>23</td>\
								</table></td>\
								<td width='10px' style='height:100%;border-width:0px 1px 0px 0px;'><div id='Calendar_Detail_Process' style='height:100%; width:100%; position:relative;'></div></td>\
								<td style='border:0'>\
									<table id='Calendar_Detail_TXT' style='height:100%; width:100%; border:0;'></table>\
								</td>\
							</tr>\
						</table>\
					</td >\
				</tr>";

	for(var i = 0; i < 6; i++){
		html +="<tr>";
		for(var j =1; j < 8; j++){
			html += "<td id='index"+(i*7 + j)+"'></td>";
		}
		html +="</tr>";
	}
	$("#Calendar").html(html);
	// 初始化标尺
	
	var today = new Date();
	Calendar_Year = today.getFullYear();
	Calendar_Month = today.getMonth() + 1;
	Calendar_Init();
});